#include "NetWork.h"
#include "timer.h"
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "aliot_connect.h"
#include "MQTTClient.h"
#ifdef USE_MODULE_MQTT
static bool s_bIsNetstart = false;
static NETWORK_STATUS   s_eNetworkStatus = NETWORK_IDLE;
static NETWORK_STATUS   s_eNetworkLastStatus = NETWORK_IDLE;

static Timer s_tNetworkTimer;

//产品密钥
static char* s_ucAliotProductKey = "a1W2Uba2nEW";
//设备名称
static char* s_ucAliotDeviceName = "TestDevice1";
//设备密钥
static char* s_ucAliotDeviceSecret = "c912e08b3aa69592656c971c1479cd2d";
//服务器地区，可在阿里云IOT控制台页面左上角和https://help.aliyun.com/document_detail/40654.html?spm=a2c4g.11186623.2.17.559573a8MfOuhn网站查看
static char* s_ucAliotRegin = "cn-shanghai";


//上层处理MQTT报文
static void MqttPubPacket(const char* topic,const char* payload,int payloadLen,int qos)
{
	printf("Receive topic:%s,payload:%s\r\n",topic,payload);
}

static void NetworkFailHandle(void)
{
    s_eNetworkLastStatus = s_eNetworkStatus;
    s_eNetworkStatus = NETWORK_WAIT;
    StartTimer(&s_tNetworkTimer,10*1000);
}

void NetworkLoop(void)
{
    switch(s_eNetworkStatus)
    {
        case NETWORK_IDLE:
            if(!s_bIsNetstart)
                return;
            if(MODULE_ERR_NONE != module_start(&g_ATModule))
            {
                NetworkFailHandle();
                return;
            }
        case NETWORK_CONNECTING:
		{
			char ucMqttUsername[256];
			char ucMqttPassword[256];
			char ucMqttClientid[256];
			char ucMqttUrl[256];
			if(NULL == module_get_ip(&g_ATModule))
            {
                NetworkFailHandle();
                return;
            }
			auth_mqtt_username(ucMqttUsername, s_ucAliotProductKey, s_ucAliotDeviceName);
			auth_mqtt_password(ucMqttPassword, s_ucAliotProductKey, s_ucAliotDeviceName,s_ucAliotDeviceSecret);
			auth_mqtt_clientid(ucMqttClientid, s_ucAliotProductKey, s_ucAliotDeviceName, "3");
			auth_mqtt_url(ucMqttUrl,s_ucAliotProductKey,s_ucAliotRegin);

            if(MODULE_ERR_NONE != module_mqtt_connect(&g_ATModule,ucMqttUrl,1883))
            {
                NetworkFailHandle();
				module_mqtt_logout(&g_ATModule);
                module_mqtt_disconnect(&g_ATModule);
                return;
            }
            if(MODULE_ERR_NONE != module_mqtt_login(&g_ATModule,ucMqttClientid,ucMqttUsername,ucMqttPassword))
            {
                NetworkFailHandle();
				module_mqtt_logout(&g_ATModule);
                module_mqtt_disconnect(&g_ATModule);
                return;
            }
            //module_mqtt_sub(&g_ATModule,"/public",1);
            s_eNetworkStatus = NETWORK_CONNECTED;
		}
        case NETWORK_CONNECTED:
            if(!s_bIsNetstart)
            {
                module_mqtt_logout(&g_ATModule);
                module_mqtt_disconnect(&g_ATModule);
                s_eNetworkStatus = NETWORK_IDLE;
			}
            break;
        case NETWORK_WAIT:
            if(TimeOut(&s_tNetworkTimer))
            {
                s_eNetworkStatus = s_eNetworkLastStatus;
            }
            break;
            default:break;
    }
	module_loop(&g_ATModule);
}

//网络初始化
void NetworkInit(void)
{
	ATModuleInit(GetTimeTick);
	module_set_mqtt_pub_callback(&g_ATModule,MqttPubPacket);
}

//启动网络
void NetworkStart(void)
{
	if(!s_bIsNetstart)
	{
		module_poweron(&g_ATModule);
		s_bIsNetstart = true;
	}
}

//停止网络
void NetworkStop(void)
{
    
    s_bIsNetstart = false;
}

//获取当前网络状态
NETWORK_STATUS GetNetworkStatus(void)
{
    return s_eNetworkStatus;
}
#endif

